package com.bbm.communitysystem.core.ai;

import org.springframework.stereotype.Component;
import org.vosk.LibVosk;
import org.vosk.LogLevel;
import org.vosk.Model;
import org.vosk.Recognizer;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.*;
import java.util.Arrays;


public class VoskModel {
    public static void main(String[] argv) throws IOException, UnsupportedAudioFileException {
        File file = new File("two.wav");

        System.out.println(file.getAbsolutePath());

        Model model = new Model("vosk-model-small-cn-0.22");



        InputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream("two.wav")));

        Recognizer recognizer = new Recognizer(model, 16000);

        int nb;

        byte[] b = new byte[4096];

        while ((nb = ais.read(b)) >= 0) {
            if (recognizer.acceptWaveForm(b, nb)) {
                System.out.println(recognizer.getResult());
            } else {
                System.out.println(recognizer.getPartialResult());
            }
        }

        System.out.println(recognizer.getFinalResult());

        model.close();
    }


}
